package com.agent.crm.view;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import xu.ye.view.GroupContactActivity;
import xu.ye.view.HomeContactActivity;
import xu.ye.view.TypeContactActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.agent.crm.base.BaseActivity;
import com.agent.crm.service.MonthYearService;
import com.agent.crm.service.SMS_Service;
import com.agent.crm.service.SechduleMainService;
import com.pwp.activity.CalendarActivity;
import com.pwp.activity.CalendarWeekActivity;
import com.pwp.activity.ContactAttentionActivity;
import com.pwp.activity.ScheduleInfoView;
import com.pwp.dao.ScheduleDAO;

public class MainActivity extends BaseActivity {
	private Button add_contacts;
	private Button title_bt_setting;
	private Button btn_contact;
	private Button btn_carlendar;
	private Button btn_search;
	private Button note_book;
	private Button btn_attention;
	
	private TextView simpleModel;
    private TextView groupModel;
	private TextView typeModel;
	private ImageButton ibtn_simpleModel;
	private ImageButton ibtn_typeModel;
	private ImageButton ibtn_groupModel;
	private ProgressDialog pdDialog; 
	
	public ArrayList<String> makeScheduleDate(String scheduleYear,String scheduleMonth,String scheduleDay,int position){
		String week = "";
		//得到这一天是星期几
        switch(position%7){
        case 0:
      	  week = "星期日";
      	  break;
        case 1:
      	  week = "星期一";
      	  break;
        case 2:
      	  week = "星期二";
      	  break;
        case 3:
      	  week = "星期三";
      	  break;
        case 4:
      	  week = "星期四";
      	  break;
        case 5:
      	  week = "星期五";
      	  break;
        case 6:
      	  week = "星期六";
      	  break;
        }
		 
        ArrayList<String> scheduleDate = new ArrayList<String>();
        scheduleDate.add(scheduleYear);
        scheduleDate.add(scheduleMonth);
        scheduleDate.add(scheduleDay);
        scheduleDate.add(week);
        //scheduleDate.add(scheduleLunarDay);
        return scheduleDate;
	}
	/*
	 * (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 * 捕捉点击事件
	 */
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bt_add_contacts:
		    break;
		case R.id.schedule_and_carlendar:
			
            Intent intent = new Intent();
            SharedPreferences preferences =getSharedPreferences("scheduleStatusMode", Context.MODE_PRIVATE);
            String lunchMode =preferences.getString("CalendarDefaultLunchMode", "WeekCalendar");
            if (lunchMode.equalsIgnoreCase("DayCalendar")) {
            	Date date = new Date();
    	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
    	    	String currentDate = sdf.format(date);  //当期日期
    	    	String year_c = currentDate.split("-")[0];
    	    	String month_c = currentDate.split("-")[1];
    	    	String day_c = currentDate.split("-")[2];
    	    	Calendar car=Calendar.getInstance();
    	    	car.setTime(new Date());
    	    	ArrayList<String> scheduleDate = makeScheduleDate(year_c, month_c, day_c,car.get(Calendar.DAY_OF_WEEK)-1);
                intent.putStringArrayListExtra("scheduleDate", scheduleDate);
            	intent.setClass(this, ScheduleInfoView.class);
			}else if(lunchMode.equalsIgnoreCase("WeekCalendar")) {
				intent.setClass(this, CalendarWeekActivity.class);
			}else if(lunchMode.equalsIgnoreCase("MonthCalendar")) {
				intent.setClass(this, CalendarActivity.class);
			}
            startActivity(intent);
			break;
		case R.id.search_contact_schedule:
            Intent intent_search = new Intent();
            intent_search.setClass(this, SearchActivity.class);
            startActivity(intent_search);
			break;
		case R.id.simpleModelId:
		case R.id.simpleModelBtnId:
            Intent simpleIntent = new Intent();
            simpleIntent.setClass(this, HomeContactActivity.class);
            startActivity(simpleIntent);
            break;
		case R.id.typeModelId:
		case R.id.typeModelBtnId:
            Intent typeIntent = new Intent();
            typeIntent.setClass(this, TypeContactActivity.class);
            startActivity(typeIntent);
            break;
        case R.id.groupModelId:
        case R.id.groupModelBtnId:
            Intent groupIntent = new Intent();
            groupIntent.setClass(this, GroupContactActivity.class);
            startActivity(groupIntent);
            break;
        case R.id.note_book:
            Intent noteIntent = new Intent();
            noteIntent.setClass(this, NoteBookActivity.class);
            startActivity(noteIntent);
            break;
        case R.id.title_bt_setting:
            Intent settignIntent =new Intent();
            settignIntent.setClass(this, SettingActivity.class);
            startActivity(settignIntent);
            break;
        case R.id.attention_contact:
            Intent aatentionIntent = new Intent();
            aatentionIntent.setClass(this, ContactAttentionActivity.class);
            startActivity(aatentionIntent);
            break;
		default:
			break;
		}

	}
	
	@Override
	protected int initLayout() {
		// TODO Auto-generated method stub
		return R.layout.main;
	}

	@Override
	protected void initViewCmp() {
		// TODO Auto-generated method stub
		add_contacts=(Button)findViewById(R.id.bt_add_contacts);
		btn_contact=(Button)findViewById(R.id.contact);
		btn_contact.setBackgroundResource(R.drawable.home_btn_bg_d);
		btn_carlendar=(Button)findViewById(R.id.schedule_and_carlendar);
		btn_search=(Button)findViewById(R.id.search_contact_schedule);
		note_book=(Button)findViewById(R.id.note_book);
		title_bt_setting=(Button)findViewById(R.id.title_bt_setting);
		btn_attention = (Button)findViewById(R.id.attention_contact);
		simpleModel = (TextView)findViewById(R.id.simpleModelId);
        groupModel = (TextView)findViewById(R.id.groupModelId);
        typeModel = (TextView)findViewById(R.id.typeModelId);
        ibtn_simpleModel = (ImageButton)findViewById(R.id.simpleModelBtnId);
        ibtn_typeModel = (ImageButton)findViewById(R.id.typeModelBtnId);
        ibtn_groupModel = (ImageButton)findViewById(R.id.groupModelBtnId);
        
        ((TextView)findViewById(R.id.title_txt)).setText("联系模块");
        findViewById(R.id.bt_add_contacts).setVisibility(View.GONE);
//        findViewById(R.id.title_bt_setting).setVisibility(View.GONE);
        SharedPreferences preferences =getSharedPreferences("delayForDBFinish", Context.MODE_PRIVATE);
//        boolean isFirstTime =preferences.getBoolean("isFirstTime",true);
//        if(isFirstTime){
//        	pdDialog =new ProgressDialog(this);
//    		pdDialog.setMessage("正在导入初始化数据，请稍候...");
//    		pdDialog.setCanceledOnTouchOutside(false);
//    		pdDialog.setCancelable(false);
//    		pdDialog.show();
//    		new Thread(new Runnable() {
//    			
//    			@Override
//    			public void run() {
//    				// TODO Auto-generated method stub
//    				try {
//    					Thread.sleep(5000);
//    					SharedPreferences preferences =getSharedPreferences("delayForDBFinish", Context.MODE_PRIVATE);
//    			        Editor editor =preferences.edit();
//    			        editor.putBoolean("isFirstTime",false);
//    			        editor.commit();
//    					mhHandler.sendEmptyMessage(0);
//    					
//    				} catch (InterruptedException e) {
//    					// TODO Auto-generated catch block
//    					e.printStackTrace();
//    				}
//    				
//    			}
//    		}).start(); 
//        }
        
	}
	
	Handler mhHandler =new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			if(null!=pdDialog&&pdDialog.isShowing()){
				pdDialog.dismiss();
			}
		}
		
	};
	

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		add_contacts.setOnClickListener(this);
		btn_carlendar.setOnClickListener(this);
		btn_search.setOnClickListener(this);
		btn_attention.setOnClickListener(this);
		simpleModel.setOnClickListener(this);
		typeModel.setOnClickListener(this);
        groupModel.setOnClickListener(this);
        note_book.setOnClickListener(this);
        title_bt_setting.setOnClickListener(this);
        ibtn_simpleModel.setOnClickListener(this);
        ibtn_typeModel.setOnClickListener(this);
        ibtn_groupModel.setOnClickListener(this);

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		SharedPreferences preferences =getSharedPreferences("login_data", Context.MODE_PRIVATE);
		boolean startAppWhenBoot= preferences.getBoolean("startAppWhenBoot", true);
		if(startAppWhenBoot){
			//不属于开机启动时，需手动启动服务
			Intent intentForRemind=new Intent(this,SechduleMainService.class);
			startService(intentForRemind);
	        Intent intentForSms=new Intent(this,SMS_Service.class);
	        startService(intentForSms);
	        Intent intentForMonthAndYearOperation=new Intent(this,MonthYearService.class);
	        startService(intentForMonthAndYearOperation);
		}
		boolean isAutoClean= preferences.getBoolean("isAutoClean", false);
		if(isAutoClean){
			new ScheduleDAO(this).clearAllFinishSchadule();
		}
		
	}

	@Override
	protected void initAsycnData() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
//		menu.add("从手机导入联系人");
//		menu.add("从文件导入联系人");+-
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected View initCustomLayout() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void changeDefaultSetting() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	protected void onDestroy() {
	    super.onDestroy();
	}

}
